﻿// ***********************************************************************
// Assembly         : STX.DAO
// Author           : seven.x
// Created          : 12-26-2013
//
// Last Modified By : seven.x
// Last Modified On : 12-27-2013
// ***********************************************************************
// <copyright file="EntityFrameworkDAL.cs" company="驰创数码">
//     Copyright (c) 驰创数码. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Configuration;
using STX.Domain;
using STX.Lib.Utils;
using STX.Lib.Web;
using System.Xml.Linq;
using Random = System.Random;

namespace STX.Repository
{
    /// <summary>
    /// Class EntityFrameworkDAL.
    /// </summary>
    /// <typeparam name="TEntity">The type of the attribute entity.</typeparam>
    internal class RepositoryProduction<TEntity>:IRepository<TEntity>
        where TEntity:BaseEntity
    {
        #region 字段
        /// <summary>
        /// The _DB context
        /// </summary>
// ReSharper disable once StaticFieldInGenericType
        private  DataBaseContext _masterDbContext;

        /// <summary>
        /// The slaver database contexts
        /// </summary>
// ReSharper disable once StaticFieldInGenericType
        private List<DataBaseContext> _slaverDbContexts;
        /// <summary>
        /// The slave start
        /// </summary>
        // ReSharper disable once StaticFieldInGenericType
        private static readonly bool SlaveStart;
        #endregion

        #region 构造函数    

        /// <summary>
        /// 静态构造函数
        /// </summary>
        static RepositoryProduction()
        {
            SlaveStart = STXConfig.GetValue("/config/dataBase/slaveStart").ToLower() == "true";
        }

        public RepositoryProduction(DataBaseContext masterContext, List<DataBaseContext> slaverDbContexts)
        {
            this._masterDbContext = masterContext;
            this._slaverDbContexts = slaverDbContexts;
        }

        #endregion

        #region 方法

        #region Method 增加

        /// <summary>
        /// Adds the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns>System.Int64.</returns>
        public void Add(TEntity entity)
        {
            _masterDbContext.Set<TEntity>().Add(entity);
 
        }
        #endregion

        #region Method 查询

        /// <summary>
        /// Finds all.
        /// </summary>
        /// <returns>IQueryable{``0}.</returns>
        public IQueryable<TEntity> FindAll()
        {
            if (!SlaveStart || _slaverDbContexts.Count == 0 || MyHttpContext.Instance.ElapseSeconds < 10)
                return _masterDbContext.Set<TEntity>().AsQueryable();
            var index = new Random(Guid.NewGuid().GetHashCode()).Next(0, _slaverDbContexts.Count);
            return _slaverDbContexts[index].Set<TEntity>().AsQueryable();
        }

        /// <summary>
        /// Finds all.
        /// </summary>
        /// <param name="exp">The exp.</param>
        /// <returns>IQueryable{``0}.</returns>
        public IQueryable<TEntity> FindAll(Func<TEntity, bool> exp)
        {
            return _masterDbContext.Set<TEntity>().Where(exp).AsQueryable();
        }

        public IQueryable<TEntity> FindAll(string sql,params object[] parameters)
        {
            if (!SlaveStart || _slaverDbContexts.Count == 0 || MyHttpContext.Instance.ElapseSeconds < 10)
                return _masterDbContext.Set<TEntity>().SqlQuery(sql, parameters).AsQueryable();
            var index = new Random(Guid.NewGuid().GetHashCode()).Next(0, _slaverDbContexts.Count);
            return _slaverDbContexts[index].Set<TEntity>().SqlQuery(sql, parameters).AsQueryable();
        }
        #endregion

        #region Method 更新
        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Update(TEntity entity)
        {
            //MasterDbContext.SaveChanges();
        }
        #endregion

        #region Method 删除
        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Remove(TEntity entity)
        {
            _masterDbContext.Set<TEntity>().Remove(entity);
        }

        #endregion

        #region 批量删除
        public void RemoveIds(int[] ids)
        {
            var myContext = _masterDbContext.Set<TEntity>();
            var entities = from s in myContext
                where ids.Contains(s.Id)
                select s;
            myContext.RemoveRange(entities);
        }
        #endregion

        #endregion



        
    }
}
